// JavaScript Document

function getStyle(obj,name)
{
	if(obj.currentStyle)
	{
		return obj.currentStyle[name];
	}
	else
	{
		return getComputedStyle(obj,false)[name];
	}
};

function startMove(obj,json,fn)
{
	clearInterval(obj.timer)	
	obj.timer=setInterval(function(){
		var bnEd=true;
		
		for(var arrt in json)
		{
			if(arrt=='opacity')
			{
				var cur=Math.round(parseFloat(getStyle(obj,arrt))*100);
			}
			else
			{
				var cur=parseInt(getStyle(obj,arrt));
			};
			
			var speed=(json[arrt]-cur)/8;
			
			speed=speed>0?Math.ceil(speed):Math.floor(speed);
			
			if(cur!=json[arrt])
			{
				bnEd=false;
			};
			if(arrt=='opacity')
			{
				obj.style.filter='alpha(opacity:'+(cur+speed)+')';
				obj.style.opacity=(cur+speed)/100;
			}
			else
			{
				obj.style[arrt]=cur+speed+'px';
			}
		};
		if(bnEd)
		{
			clearInterval(obj.timer);
			if(fn)fn();
		}
	},30);
};


var bn=false;
var menu1sSrcId = [];

function reorderMenu(menu1sIdStr){
	if(menu1sIdStr == null || menu1sIdStr == "") return;
	var menu1sDesId = menu1sIdStr.split(",");
	
	for(var j = 0;j < menu1sSrcId.length && j < menu1sDesId.length; j++){
		if(menu1sSrcId[j] == menu1sDesId[j]) continue;
		
		var menu1Src = document.getElementById(menu1sSrcId[j]);
		var menu1Des = document.getElementById(menu1sDesId[j]);
		
		var tmpl = menu1Src.style.left;
		var tmpt = menu1Src.style.top;
		menu1Src.style.left = menu1Des.style.left;
		menu1Src.style.top = menu1Des.style.top;
		menu1Des.style.left = tmpl;
		menu1Des.style.top = tmpt;
		
		for(var k = j + 1;k < menu1sSrcId.length; k++){
			if(menu1sSrcId[k] == menu1sDesId[j]){
				menu1sSrcId[k] = menu1sSrcId[j];
				menu1sSrcId[j] = menu1sDesId[j];
				menu1Src.index = k;
				menu1Des.index = j;
				break;
			}
		}
	}
}

function menuload()
{	
	var oMo=document.getElementById('menu1')
	var oNc=document.getElementById('showAllMenu')
	var oUl=document.getElementById('menu1Content');
	var showAllMenuDiv=document.getElementById('showAllMenuDiv');
	var aLi=oUl.getElementsByTagName('a');
	var comHieght = parseInt(oMo.offsetHeight);
	var lineCount =Math.ceil(aLi.length/8);
	oNc.onclick=function()
	{
		if(bn){
			oUl.style.height=comHieght+'px';
			oMo.style.height=comHieght+'px';
			bn=false;
			showAllMenuDiv.className = "openAllMenu_div";
		}else{
			oMo.style.height= (comHieght * lineCount) + 'px';
			oUl.style.height= (comHieght * lineCount) + 'px';
			bn=true;
			showAllMenuDiv.className = "closeAllMenu_div";
		}

	};
	
	var aPos=[];
	
	var zIndex=2;
	
	for(var i=0;i<aLi.length;i++)
	{
		aPos[i]={left: aLi[i].offsetLeft, top: aLi[i].offsetTop};
		menu1sSrcId.push(aLi[i].id);
	};
	
	for(var i=0;i<aLi.length;i++)
	{
		aLi[i].style.left=aPos[i].left+'px';
		aLi[i].style.top=aPos[i].top+'px';
		
		aLi[i].style.position='absolute';
		aLi[i].style.margin='0';
		
		aLi[i].index=i;
		
		setDrag(aLi[i])
		
	}
	
	function setDrag(obj)
	{
		
		obj.onmousedown=function (ev)
		{
			if(!bn)return;
			var oEvent=ev||event;
			
			var disX=oEvent.clientX-obj.offsetLeft;
			var disY=oEvent.clientY-obj.offsetTop;
			
			obj.style.zIndex=zIndex++;
			
			clearInterval(obj.timer);
			
			document.onmousemove=function (ev)
			{
				var oEvent=ev||event;
				var x=oEvent.clientX-disX;
				var y=oEvent.clientY-disY;
				if(x<0)
				{
					x=0;
				}
				else if(x>oUl.offsetWidth-obj.offsetWidth)
				{
					x=oUl.offsetWidth-obj.offsetWidth
				}
				if(y<0)
				{
					y=0;
				}
				else if(y>oUl.offsetHeight-obj.offsetHeight)
				{
					y=oUl.offsetHeight-obj.offsetHeight
				}
				obj.style.left=x+'px';
				obj.style.top=y+'px';
				
				
			};
			
			document.onmouseup=function ()
			{
				document.onmousemove=null;
				document.onmouseup=null;
				
				var oNear=getNearest(obj);
				if(oNear)	//碰上了
				{
					menu1sSrcId[obj.index] = oNear.id;
					menu1sSrcId[oNear.index] = obj.id;
					
					startMove(oNear, aPos[obj.index]);
					startMove(obj, aPos[oNear.index]);
					
					var tmp=0;
					
					tmp=obj.index;
					obj.index=oNear.index;
					oNear.index=tmp;

				}
				else	//谁都没碰上
				{
					startMove(obj, aPos[obj.index]);
				};
				if(obj.releaseCapture)
				{
					obj.releaseCapture();
				}
			};
			if(obj.setCapture)
			{
				obj.setCapture();
			}
			return false;
		};
		
	}
	
	function getDis(obj1, obj2)	//求距离
	{
		var a=obj1.offsetLeft-obj2.offsetLeft;
		var b=obj1.offsetTop-obj2.offsetTop;
		
		return Math.sqrt(a*a+b*b);
	}
	
	function test(obj1, obj2)	//碰撞检测
	{
		var l1=obj1.offsetLeft;
		var t1=obj1.offsetTop;
		var r1=obj1.offsetLeft+obj1.offsetWidth;
		var b1=obj1.offsetTop+obj1.offsetHeight;
		
		var l2=obj2.offsetLeft;
		var t2=obj2.offsetTop;
		var r2=obj2.offsetLeft+obj2.offsetWidth;
		var b2=obj2.offsetTop+obj2.offsetHeight;

		if(l1>r2 || t1>b2 || r1<l2 || b1<t2)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
	function getNearest(obj)	//找出最近的物体
	{
		//1.碰上
		//2.最近的
		var min=9999999;
		var minIndex=-1;
		
		for(var i=0;i<aLi.length;i++)
		{
			if(obj==aLi[i])continue;
			
			if(test(obj, aLi[i]))
			{
				var dis=getDis(obj, aLi[i]);
				if(dis<min)
				{
					min=dis;
					minIndex=i;
				}
			}
		}
		
		if(minIndex==-1)
		{
			//没碰上
			return null;
		}
		else
		{
			return aLi[minIndex];
		}
	}

}

var lin="";
var str="";
var i=1;
var x,y;
function dragMenu(menuObj){
	document.body.style.cursor = 'move';
	var str = $(menuObj).text();
	lin=$(menuObj).attr("href");
	$("#lo").html(str);
	$("#lo").show();
	x=event.clientX;
	y=event.clientY;
	$("#lo").css('left',x-25);
	$("#lo").css('top',y-15);
	showSideBar();
}
function showSideBar(){
	$("#sidebarMain").css({left:"-200px"}).show();
	var i = $("#sideLinkSdr").find(".sbLink_div").length;
	if(i < 5){
		sidelink.Run(0, true);
	}else if (i>=5 && i < 10){
		sidelink.Run(1, true);
	}else if (i>=10 && i <15){
		sidelink.Run(2, true);
	}
	
}
function hideSideBar(){
	$("#sidebarMain").css({left:"27"}).hide();
}
var linkPername = "LK_"
function releaseMenuu(menuObj){
	document.body.style.cursor = 'auto';
	
	var $sidebar = $("#sidebarMain");
	var bnFlag=true;
	var X=event.clientX;
	var Y=event.clientY;

	var minX = $sidebar.offset().left;
	var minY = $sidebar.offset().top;
	var maxX = $sidebar.offset().left + $sidebar.width();
	var maxY = $sidebar.offset().top + $sidebar.height();

	if(X<minX||X>maxX||Y<minY||Y>maxY){
		bnFlag=false;
		hideSideBar();
	}
	while(bnFlag){
		var linkId = linkPername + menuObj.id;
		var isExist = false;
		var $links = $("#sideLinkSdr").find(".sbLink_div");
		$links.each(function(){
			if(this.id == linkId){
				alert("您所选的快捷方式已经存在!");
				isExist = true;
				hideSideBar();
				return false;
			}
		})
		if(isExist){
			break;
		}
		if($links.length>=15){
			alert("最多只能放置15个快捷方式!");
			hideSideBar();
			break;
		}
		
		addSidebarLink(menuObj.id)
		break;
	}
	$("#lo").hide()
	str="";
	$("#lo").html(str);
}
function addSidebarLink(menuId){
	var newId = linkPername + $("#"+menuId).attr('id');
	var newText = $("#"+menuId).text();
	var linkhtml = '<div class="sbLink_div" id="' + newId + '" onclick="clickLink(this)"><span>'+ newText +'</span><a class="sbLinkClose_a" href="javascript:" onclick="colseLink(this)"></a></div>';
	var i = $("#sideLinkSdr").find(".sbLink_div").length;
	if(i < 5){
		$("#sbPage1").append(linkhtml);
	}else if (i>=5 && i < 10){
		$("#sbPage2").append(linkhtml);
	}else if (i>=10 && i <15){
		$("#sbPage3").append(linkhtml);
	}
	
}

function clickLink(obj){
	var srcId = obj.id.substring(linkPername.length,obj.id.length);
	$("#"+srcId).click();
}

function colseLink(obj){
	$(obj).parent().remove();
}